#include "StdAfx.h"
#include "rtfwriter.h"

CStringA CRTFWriter::RTFFontFamily[]={
	"nill","roman","swiss","modern","script","decor","tech","bidi"};
char CRTFWriter::JstChar[]={'l','r','c','j'};

void CRTFWriter::StartDocument(RichLanguage Lang)
{
	StateLang = Lang;
	if(Lang==CRichWriter::Arabic)
        Write("{\\rtf1\\adeflang3073\\ansi\\ansicpg1256\r\n");
	else
		Write("{\\rtf1\\adeflang1033\\ansi\\ansicpg1200\r\n");
}
void CRTFWriter::StartDocHeader(CStringA Title, CStringA Author)
{
}
void CRTFWriter::StartFontTable()
{
	Write("\\deff0{\\fonttbl\r\n");
}
void CRTFWriter::AddFont(FontFamily Family, CStringA Face
	,int Charset, int Codepage)
{
	CStringA fontitem;
	fontitem.Format("{\\f%d\\f%s %s;}\r\n",LastFont++,RTFFontFamily[(int)Family],Face);
	Write(fontitem);
}
void CRTFWriter::EndFontTable()
{
	Write("}\r\n");
}
void CRTFWriter::StartColorTable()
{
	Write("{\\colortbl\r\n");
}
void CRTFWriter::AddColor(int Red, int Green, int Blue)
{
	CStringA fontitem;
	fontitem.Format("\\red%d \\green%d \\blue%d ;",Red,Green,Blue);
	Write(fontitem);
}
void CRTFWriter::EndColorTable()
{
	Write("}\r\n");
}
void CRTFWriter::EndDocHeader()
{
}
void CRTFWriter::StartHeader()
{
	Write("{\\*\\header ");
}
void CRTFWriter::EndHeader()
{
	Write("}");
}
void CRTFWriter::StartFooter()
{
	Write("{\\*\\footer ");
}
void CRTFWriter::EndFooter()
{
	Write("}");
}
void CRTFWriter::AddPageNumber()
{
Write("\\chpgn ");
}
void CRTFWriter::StartParagraph(RichLanguage Lang, RichJust Just)
{
	Write("\\pard ");
	if(bInTable) Write("\\intbl ");
	if(Lang==CRichWriter::Arabic)
		Write("\\rtlpar ");
	else
		Write("\\ltrpar ");
	if(Just!=NotSpec)
	{
		Write("\\q");
		Write(JstChar[(int)Just]);
	}
	Write("\r\n");
	SetLang(Lang);
}
void CRTFWriter::StartFootnote()
{
	Write("{\\footnote ");
}
void CRTFWriter::EndFootnote()
{
	Write("}");
}
void CRTFWriter::SetLang(RichLanguage Lang)
{
	StateLang = Lang;
	if(Lang==CRichWriter::Arabic)
		Write("\\rtlch ");
	else
		Write("\\ltrch ");
}
void CRTFWriter::SetFont(int n)
{
	CStringA f;
	f.Format("\\f%d ",n);
	Write(f);
}
void CRTFWriter::SetSize(int n)
{
	CStringA f;
	f.Format("\\fs%d ",n);
	Write(f);
}
void CRTFWriter::SetColor(int n)
{
	CStringA f;
	f.Format("\\cf%d ",n);
	Write(f);
}
void CRTFWriter::SetBold(BOOL bBold)
{
	Write(bBold ? "\\b " : "\\b0 ");
}
void CRTFWriter::SetUnderline(BOOL bUnderline)
{
	Write(bUnderline ? "\\ul " : "\\ul0 ");
}
void CRTFWriter::AddChar(char chr)
{
	if(chr=='\\')
		Write("\\\\");
	else if(chr=='{')
		Write("\\{");
	else if(chr=='}')
		Write("\\}");
	else if(StateLang==CRichWriter::Arabic&&
		chr>=0x30&&
		chr<=0x39)
	{
		int c = chr-0x30;
		Write("\\u16");
		if(c>7) {Write('4');Write(char(0x28)+c);}
		else {Write('3');Write(char(0x32)+c);}
		Write("? ");
	}
	else
		Write(chr);
}
void CRTFWriter::AddUnicodeChar(WCHAR chr)
{
	CStringA f;
	f.Format("\\u%04d?",chr,(char)chr);
	Write(f);
}
void CRTFWriter::AddTab()
{
	Write("\\tab ");
}
void CRTFWriter::AddHardSpace()
{
	Write("\\~ ");
}
void CRTFWriter::EndParagraph()
{
	if(!bInTable) Write("\\par\r\n");
}
void CRTFWriter::StartTable()
{
	Write("\\trowd ");
}
void CRTFWriter::StartTableDef()
{
	RowWidth = 0;
}
void CRTFWriter::DefCell(int twpWidth)
{
	CStringA f;
	f.Format("\\cellx%d ",twpWidth + RowWidth);
	Write(f);
	RowWidth += twpWidth;
}
void CRTFWriter::EndTableDef()
{
	Write("\n\r");
}
void CRTFWriter::StartRow()
{
}
void CRTFWriter::StartCell()
{
	bInTable=TRUE;
}
void CRTFWriter::EndCell()
{
	Write("\\cell\r\n");
	bInTable=FALSE;
}
void CRTFWriter::EndRow()
{
	Write("\\row\r\n");
}
void CRTFWriter::EndTable()
{
}
void CRTFWriter::EndDocument()
{
	Write("}");
}

void CRTFWriter::SetPageNumber(int n)
{
	CStringA f;
	f.Format("\\pgnstarts%d ",n);
	Write(f);
}
void CRTFWriter::PageAdvance()
{
	Write("\\page\r\n");
	// \insrsid1 to make Word do Advance page (??!!!) (not documented)
	// \par to do advance before table (???!!!!)
}

